<?php

namespace app\admin\controller;

use think\Db;
use app\common\validate\GoodsValidate;

class Goods extends Base
{
	protected $datas;
	
	/**
	 * 商品列表
	 * @Author   Yichen
	 * @DateTime 2020-05-02T15:24:54+0800
	 * @return   [type]                   [description]
	 */
	public function goodsList()
	{
		$this->datas = $this->params;
		if (isset($this->datas['category_id']) && isset($this->datas['name']) && isset($this->datas['price'])) {
			//搜索
			$categoryId = $this->datas['category_id'];
			$name = $this->datas['name'];
			$price = $this->datas['price'];
			$goodsData = $this->_search($categoryId, $name, $price);
		} else {
			$goodsData = Db::name('t_goods')->alias('g')
				->join('t_category c', 'g.category_id=c.id', 'left')
				->field('g.*,c.name categoryName')
				->paginate(10);
		}
		$this->assign('list', $goodsData);
		$categoryData = Db::name('t_category')->field('id,name')->select();
		$this->assign('categoryData', $categoryData);
		return $this->fetch();
	}

	/**
	 * 添加
	 * @Author   Yichen
	 * @DateTime 2020-05-02T17:14:35+0800
	 */
	public function addGoods()
	{
		if (request()->isPost()) {
			$this->datas = $this->params;
			$addValidate = new GoodsValidate();
			if (!$addValidate->scene('add')->check($this->datas)) {
				$this->error($addValidate->getError());
			}
			$insertData['name'] = $this->datas['name'];
			$insertData['price'] = $this->datas['price'];
			$insertData['cover_img'] = $this->datas['cover_img'];
			$insertData['description'] = $this->datas['description'];
			$insertData['category_id'] = $this->datas['category_id'];
			$insertData['poster'] = isset($this->datas['poster']) ? $this->datas['poster'] : '';
			$insertData['create_time'] = time();
			$result = Db::name('t_goods')->insertGetId($insertData);
			if (!$result) {
				$this->error('添加失败');
			}
			$this->success('添加成功', '', $result);
		}
		$categoryData = Db::name('t_category')->field('id,name')->select();
		$this->assign('categoryData', $categoryData);
		return $this->fetch();
	}


	/**
	 * 上传图片
	 * @Author   fanzhongxing
	 * @DateTime 2020-05-01T14:09:27+0800
	 * @return   [type]                   [description]
	 */
	public function uploadImg()
	{
		$path = 'static'. DS . 'upload' . DS . 'goods';
		$size = 58400*100;
		$type = 'jpg,png,gif,jpeg';
		$uploadResult = uploadFile('file', $path, $size, $type);
		if (!$uploadResult[0]) {
			$this->error($uploadResult[1]);
		}
		$this->success('上传成功', '', $uploadResult[1]);
	}

	/**
	 * 保存商品详情 
	 * @Author   Yichen
	 * @DateTime 2020-05-03T10:23:21+0800
	 * @return   [type]                   [description]
	 */
	public function saveGoodsProperty()
	{
		if (request()->isPost()) {
			$this->datas = $this->params;
			$validate = new GoodsValidate();
			if (!$validate->scene('addProperty')->check($this->datas)) {
				$this->error($validate->getError());
			}
			$data['goods_id'] = $this->datas['gid'];
			$data['specifications_name'] = $this->datas['specifications_name'];
			$data['price'] = $this->datas['price'];
			$data['big_images'] = $this->datas['big_images'];
			$data['stock'] = $this->datas['stock'];
			$data['remark'] = isset($this->datas['remark']) ? $this->datas['remark'] : '';
			if (isset($this->datas['id'])) {
				$id = $this->datas['id'];
				$data['update_time'] = time();
				$result = Db::name('t_goods_property')->where('id', $id)->update($data);
			} else {
				$data['create_time'] = time();
				$result = Db::name('t_goods_property')->insert($data);
			}
			if (!$result) {
				$this->error('失败');
			}
			$this->success('成功');
		}
		$this->error('非法请求');
	}

	/**
	 * 完成添加
	 * @Author   Yichen
	 * @DateTime 2020-05-03T10:37:24+0800
	 * @return   [type]          1上架         [description]
	 */
	public function finish()
	{
		$this->datas = $this->params;
		$validate = new GoodsValidate();
		if (!$validate->scene('finish')->check($this->datas)) {
			$this->error($validate->getError());
		}
		$gid = $this->datas['gid'];
		$type = $this->datas['type'];
		if ($type == 1) {
			$result = Db::name('t_goods')->where('id', $gid)->update(['status' => 1]);
			if (!$result) {
				$this->error('失败');
			}
			$this->success('成功');
		}
		$this->success('成功');
	}


	/**
	 * 修改
	 * @Author   Yichen
	 * @DateTime 2020-05-03T15:04:42+0800
	 * @return   [type]                   [description]
	 */
	public function editGoods()
	{
		$this->datas = $this->params;
		$gid = $this->datas['gid'];
		if(request()->isPost()) {
			$updateData['id'] = $gid;
			$updateData['name'] = $this->datas['name'];
			$updateData['price'] = $this->datas['price'];
			$updateData['cover_img'] = $this->datas['cover_img'];
			$updateData['description'] = $this->datas['description'];
			$updateData['category_id'] = $this->datas['category_id'];
			$updateData['poster'] = isset($this->datas['poster']) ? $this->datas['poster'] : '';
			$updateData['update_time'] = time();
			$result = Db::name('t_goods')->where('id', $gid)->update($updateData);
			if (!$result) {
				$this->error('失败');
			}
			$this->success('成功');
		}
		$goodsData = Db::name('t_goods')->where('id', $gid)->find();
		$this->assign('data', $goodsData);
		$categoryData = Db::name('t_category')->select();
		$this->assign('categoryData', $categoryData);
		$goodsProData = Db::name('t_goods_property')->where('goods_id', $gid)->select();
		$this->assign('list', $goodsProData);
		return $this->fetch();
	}

	/**
	 * 删除商品详情
	 * @Author   Yichen
	 * @DateTime 2020-05-03T21:43:05+0800
	 * @return   [type]                   [description]
	 */
	public function delGoodsInfo()
	{
		if (request()->isGet()) {
			$this->datas = $this->params;
			$id = $this->datas['id'];
			$result = Db::name('t_goods_property')->where('id', $id)->delete();
			if (!$result) {
				$this->error('失败');
			}
			$this->success('成功');
		}
		$this->error('非法请求');
	}

	/**
	 * 上架或下架
	 * @Author   fanzhongxing
	 * @DateTime 2020-05-01T16:16:12+0800
	 */
	public function ProhibitOrEnable()
	{
		if (request()->isGet()) {
			$this->datas = $this->params;
			$ProOrEnaValidate = new GoodsValidate();
			if (!$ProOrEnaValidate->scene('ProOrEna')->check($this->datas)) {
				$this->error($ProOrEnaValidate->getError());
			}
			$gid = $this->datas['gid'];
			$msg = $this->datas['msg'];
			if ($msg == '下架') {
				$status = 0;
			} else {
				$status = 1;
			}
			$result = Db::name('t_goods')->where('id', $gid)->update(['status' => $status]);
			if (!$result) {
				$this->error('失败');
			}
			$this->success('成功');
		}
		$this->error('非法请求');
	}

	/**
	 * 删除
	 * @Author   Yichen
	 * @DateTime 2020-05-04T14:06:20+0800
	 * @return   [type]                   [description]
	 */
	public function delGoods()
	{
		if (request()->isGet()) {
			$this->datas = $this->params;
			$validate = new GoodsValidate();
			if (!$validate->scene('del')->check($this->datas)) {
				$this->error($validate->getError());
			}
			$gid = $this->datas['gid'];
			Db::startTrans();
			try {
			    Db::name('t_goods')->where('id', $gid)->delete();
			    Db::name('t_goods_property')->where('goods_id', $gid)->delete();
			    Db::commit();
			} catch (\Exception $e) {
			    Db::rollback();
			    $this->error('失败');
			}
			$this->success('删除成功');
		}
		$this->error('非法请求');
	}

	/**
	 * 搜索
	 * @Author   Yichen
	 * @DateTime 2020-05-05T09:10:02+0800
	 * @param    [type]                   $categoryId [description]
	 * @param    [type]                   $name       [description]
	 * @param    [type]                   $price      [description]
	 * @return   [type]                               [description]
	 */
	protected function _search($categoryId, $name, $price) 
	{
		$where = [];
		if ($categoryId != 0 && !empty($name) && !empty($price)) {
			$where = [
				['g.category_id', '=', $categoryId],
				['g.name', 'like' , '%' . $name .'%'],
				['g.price', $price]
			];
		} else if ($categoryId == 0 && !empty($name) && !empty($price)) {
			$where = [
				['g.name', 'like' , '%' . $name .'%'],
				['g.price', '=', $price]
			];
		} else if ($categoryId == 0 && empty($name) && !empty($price)) {
			$where = [
				'g.price' => $price
			];
		} else if ($categoryId == 0 && !empty($name) && empty($price)) {
			$where = [
				['g.name', 'like', '%' . $name .'%']
			];
		}
		$data = Db::name('t_goods')->alias('g')
			->join('t_category c', 'g.category_id=c.id', 'left')
			->where($where)
			->field('g.*,c.name categoryName')
			->paginate(10);
		return $data;
	}
}